package com.example.refreshbanner;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.adapter.HomeNewsAdapter;
import com.example.bean.HomeBean;
import com.example.util.StatusBarUtils;
import com.example.widget.BannerHeaderView;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnMultiListener;
import com.scwang.smart.refresh.layout.simple.SimpleMultiListener;
import com.scwang.smart.refresh.layout.util.SmartUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private int mOffset = 0;
    private int mScrollY = 0;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Toolbar toolbar = findViewById(R.id.id_toolbar);
        final LinearLayout layoutAlpha = findViewById(R.id.layoutAlpha);
        final RefreshLayout refreshLayout = findViewById(R.id.id_refreshLayout);
        final NestedScrollView scrollView = findViewById(R.id.id_scrollview);
        final RecyclerView recyclerView = findViewById(R.id.id_recyclerView);

        //状态栏透明和间距处理
        StatusBarUtils.immersive(this);
        StatusBarUtils.setPaddingSmart(this, toolbar);
        StatusBarUtils.darkMode(this);


        List<HomeBean> homeList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            if (i == 0) {
                homeList.add(new HomeBean(HomeBean.IMG, "itemName=" + i));
            } else {
                homeList.add(new HomeBean(HomeBean.TEXT, "itemName=" + i));
            }
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        final HomeNewsAdapter adapter = new HomeNewsAdapter();
        View headerView = LayoutInflater.from(this).inflate(R.layout.banner_header, null);
        adapter.addHeaderView(headerView);
        adapter.addData(homeList);
        recyclerView.setAdapter(adapter);

        refreshLayout.setFixedHeaderViewId(R.id.id_toolbar);
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnMultiListener(new SimpleMultiListener() {
            @Override
            public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                mOffset = offset / 2;
                layoutAlpha.setAlpha(1 - Math.min(percent, 1));
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                refreshLayout.finishRefresh();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                List<HomeBean> loadList = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    loadList.add(new HomeBean(HomeBean.TEXT, "loadItem:" + i));
                }
                adapter.addData(loadList);
                refreshLayout.finishLoadMore(1000);
            }
        });

        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            private int lastScrollY = 0;
            private int h = SmartUtil.dp2px(50);
            private int color = ContextCompat.getColor(getApplicationContext(), R.color.white)&0x00ffffff;
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.d(TAG,"scrollPosition==" + scrollY);
                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
                }
                if (scrollY <= 0) {
                    toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
                }
                lastScrollY = scrollY;
            }
        });

//        toolbar.setBackgroundColor(0);
    }
}